<template>
  <div class="emp-manager">
    <h1>广告编辑</h1>
    <el-divider></el-divider>
    <!-- 数据修改和添加 -->
    <el-form :model="adver" align="left">
      <el-form-item label="所属分类" :label-width="formLabelWidth">
        <el-select v-model="adver.categoryId" placeholder="请选择分类">
          <el-option
            v-for="c in cateList"
            :label="c.name"
            :value="c.id"
            :key="c.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="广告标题" :label-width="formLabelWidth">
        <el-input v-model="adver.title" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="广告地址" :label-width="formLabelWidth">
        <el-input v-model="adver.url" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="海报" :label-width="formLabelWidth">
        <el-upload
          class="upload-demo"
          action="http://adver.daxigua.com/api/oss/upload"
          :before-upload="beforeHandle"
          :on-success="afterHandle"
          :on-error="afterError"
          list-type="picture">
          <el-button size="small" type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
        </el-upload>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="toListPage">取 消</el-button>
      <el-button type="primary" @click="confirmSave" :disabled="butDisable"
        >确 定</el-button
      >
    </div>
  </div>
</template>
<script>
import adverApi from "@/api/adver/adver";
import cateApi from "@/api/adver/cate";

export default {
  data() {
    return {
      formLabelWidth: '100px',
      adver: {},
      butDisable: false,
      cateList: [],
    };
  },

  created() {
    this.adver.id = this.$route.query.id;
    this.getAllCate();
    if (this.adver.id) {
      this.getById();
    }
  },

  methods: {
    afterError(err) {
      console.log(err);
      
    },
    beforeHandle(file) {
      console.log(file);
      
    },
    afterHandle(resp) {
      this.adver.pic = resp.data.imgUrl;
    },
    getAllCate() {
        cateApi.getAllCate().then(resp => {
            this.cateList = resp.data.list;
        })
    },

    getById() {
        adverApi.getAdverById(this.adver.id).then((resp) => {
            this.adver = resp.data.adver;
        })
    },

    toListPage() {
      this.$router.push("/adver/list");
    },

    //确认添加/修改
    confirmSave() {
      if (this.adver.id) {
        //有id则为修改
        this.updateAdvertisement();
      } else {
        this.addAdvertisement();
      }

      this.toListPage()
    },

    //添加广告
    addAdvertisement() {
      adverApi.saveAdver(this.adver).then((resp) => {
        if (resp.code == 0) {
          this.$message({
            type: "success",
            message: "添加成功!",
          });
        }
      });
    },

    //修改广告
    updateAdvertisement() {
      adverApi.updateAdver(this.adver).then((resp) => {
        if (resp.data.code == 0) {
          this.$message({
            type: "success",
            message: "修改成功!",
          });
        }
      });
    },

    getAdver(id) {
      adverApi.getAdver(id).then((resp) => {
        this.adver = resp.data.data.adver;
      });
    },
  },
};
</script>
